<?php
/*
 * Created on Dec 9, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
	/**
	 * 
	 * $imagePath = path of the image
	 * $center = LatLng of the antenna
	 * $swBound = LatLng of the sw bound of the initial image
	 * $neBound = LatLng of the ne bound of the initial image
	 * $shrink = the shrinkage factor ( 0 < X < 1 )
	 * $
	 */
	function Crop($imagePath, $center, $swBound, $neBound, $shrink = .1){
				
		$I_N_Lat = $neBound->lat;
		$I_E_Lng = $neBound->lng;
		$I_S_Lat = $swBound->lat;
		$I_W_Lng = $swBound->lng;
		
    // get size of the original
    $imginfo = getimagesize($imagePath);
    $pixWidth = $imginfo[0];
    $pixHeight = $imginfo[1];
		
//  throw new Exception("Path: " . $imagePath . " center: (" . $center->lat . "," . $center->lng . ")" . " N:". $I_N_Lat . " S:" . $I_S_Lat . " E:" . $I_E_Lng . " W:" . $I_W_Lng);
		
		// Get geogrpahic bounds of the original
		$I_Lat_Height = abs($I_N_Lat - $I_S_Lat);
		$I_Lng_Width = abs($I_E_Lng - $I_W_Lng);
		
//	throw new Exception("Lat_Height: " . $I_Lat_Height . ", Lng_Width: " . $I_Lng_Width);
		
		// Calculate the ratio of pixels to coord lines
		$LatPerPix = $I_Lat_Height / $pixHeight;
		$LngPerPix = $I_Lng_Width / $pixWidth;
		
		// Calculate center pixel
		$center_x = floor((($center->lng - $I_W_Lng)/$I_Lng_Width) * $pixWidth);
		$center_y = floor((($I_N_Lat - $center->lat)/$I_Lat_Height) * $pixHeight);
		
//		throw new Exception("Center_X: " . $center_x . ", Center_Y: " . $center_y);
		
		// Calculate pixel boundaries of new image
		$n_pix = $center_y - ( $pixHeight * ($shrink / 2) );
		$s_pix = $center_y + ( $pixHeight * ($shrink / 2) );
		$w_pix = $center_x - ( $pixWidth * ($shrink / 2) );
		$e_pix = $center_x + ( $pixWidth * ($shrink / 2) );
		
//		throw new Exception("N_Pix: " . $n_pix . ", S_Pix: " . $s_pix . ", E_Pix: " . $e_pix . ", W_Pix: " . $w_pix );
	
	
		// Calculate geographic bounds of new image
		$F_N = min($I_N_Lat - ( $n_pix * $LatPerPix ), $I_N_Lat);
		$F_S = max($I_N_Lat - ( $s_pix * $LatPerPix ), $I_S_Lat);
		$F_W = max($I_W_Lng + ( $w_pix * $LngPerPix ), $I_W_Lng);
		$F_E = min($I_W_Lng + ( $e_pix * $LngPerPix ), $I_E_Lng);

		// Set the new image offset from the original (nw corner)
		$offset_y =  $n_pix;      //(abs($I_N_Lat) - abs($F_N_Lat)) / $LatPerPix; //(abs($I_N_Lat) - abs($F_N_Lat)) * $pixPerLat;
		$offset_x =  $w_pix;      //(abs($I_W_Lng) - abs($F_W_Lng)) / $LngPerPix; //(abs($F_W_Lng) - abs($I_W_Lng)) * $pixPerLng;
		
		// Calculate the new images dimensions
		$F_Width = min(floor($pixWidth * $shrink), ($pixWidth - $w_pix)); //floor($I_Lng_Width * $shrink) * $pixPerLng;
		$F_Height = min(floor($pixHeight * $shrink), ($s_pix)); //floor($I_Lat_Height * $shrink) * $pixPerLat;
		
		// Create the command line string
		$command = "convert " . $imagePath . " -crop " . 
									$F_Width . "x" . $F_Height . "+" . 
									$offset_x . "+" . $offset_y . " " . $imagePath; 
		
//		throw new Exception($command);
		
		// Run command
		system($command);
		
		// Set Return Variables
		$ne = new LatLng($F_N, $F_E);
		$sw = new LatLng($F_S, $F_W);
		$newImage = new Image($imagePath, $ne, $sw);
		return $newImage;
		
	}
	
?>
